//
//  WKBaseContenListCell.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/5.
//  Copyright © 2018年 何文虎. All rights reserved.
//

#import "WKBaseContenListCell.h"
#import "WKCommentView.h"
#import "WKContenCellPhotoView.h"
#import "WKContenCellViedeoView.h"
#import "WKContenCellRewardView.h"
#import "WKContenCellQuestionView.h"

CGFloat maxContentLabelHeight = 0; // 根据具体font而定

@interface WKBaseContenListCell ()<SDPhotoBrowserDelegate>
{
    UIImageView *_headerImageview;
    UILabel *_nameLb;
    UILabel *_memberLb;
    UILabel *_timeLb;
    UILabel *_contentLb;
    UIButton *_guanzhuBtn;
    
    UIView *_contenView;
    WKCommentView *_commentView;
    WKContenCellRewardView *_rewardView;
}

@end

@implementation WKBaseContenListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setup];
        
        //设置主题
        [self configTheme];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

//设置主题
- (void)configTheme{
    
}


- (void)setup{
    _headerImageview = [[UIImageView alloc]init];
    
    _nameLb = [UILabel new];
    _nameLb.font = PFSC_MediumFont(15);
    _nameLb.textColor = UIColorFromHex(0x3c3c3c);
    
    _memberLb = [UILabel new];

    _timeLb = [UILabel new];
    _timeLb = [UILabel new];
    _timeLb.font = PFSC_MediumFont(11);
    _timeLb.textColor = UIColorFromHex(0x979797);
    
    
    _guanzhuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_guanzhuBtn setTitle:@"+关注" forState:UIControlStateNormal];
    [_guanzhuBtn setTitle:@"已关注" forState:UIControlStateSelected];
    [_guanzhuBtn setTitleColor:UIColorFromHex(0xF54050) forState:UIControlStateNormal];
    [_guanzhuBtn setTitleColor:UIColorFromHex(0xFFFFFF) forState:UIControlStateSelected];
    _guanzhuBtn.titleLabel.font = PFSC_RegularFont(11);
    _guanzhuBtn.layer.masksToBounds = YES;
    _guanzhuBtn.layer.cornerRadius = 5;
    _guanzhuBtn.layer.borderWidth = 1;
    _guanzhuBtn.layer.borderColor = UIColorFromHex(0xF54050).CGColor;
    [_guanzhuBtn addTarget:self action:@selector(guanzhuClick:) forControlEvents:UIControlEventTouchUpInside];
    _guanzhuBtn.backgroundColor = [UIColor whiteColor];
    
    
    _contentLb = [UILabel new];
    _contentLb.font = PFSC_RegularFont(15);
    _contentLb.textColor = UIColorFromHex(0x3c3c3c);
   
    
    _contenView = [UIView new];
    _commentView = [WKCommentView new];
    _rewardView = [WKContenCellRewardView new];
    
    NSArray *views = @[_headerImageview,_nameLb,_memberLb,_timeLb,_contentLb,_contenView,_commentView,_rewardView,_guanzhuBtn];
    
    
    [self.contentView wh_addSubviews:views];
    
    UIView *contentView = self.contentView;
    CGFloat margin = 12;
    
    [_headerImageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(margin);
        make.top.mas_offset(margin);
        make.size.mas_equalTo(CGSizeMake(36, 36));
    }];

    
    [_nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_headerImageview.mas_right).offset(margin);
        make.top.mas_equalTo(_headerImageview.mas_top);
        make.size.mas_equalTo(CGSizeMake(80, 14));
    }];

    
    [_memberLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_nameLb.mas_right).offset(10);
        make.top.mas_equalTo(_nameLb.mas_top);
        make.size.mas_equalTo(CGSizeMake(50, 12));
    }];

    
    [_timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_nameLb.mas_left);
        make.top.mas_equalTo(_nameLb.mas_bottom).offset(8);
        make.size.mas_equalTo(CGSizeMake(200, 14));
    }];

    
    [_guanzhuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-margin);
        make.top.mas_equalTo(17);
        make.size.mas_equalTo(CGSizeMake(50, 24));
    }];

    
    [_contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
         make.right.mas_equalTo(-margin);
        make.top.mas_equalTo(_headerImageview.mas_bottom).offset(margin);
       
    
    }];

    
    [_contenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.top.mas_equalTo(_contentLb.mas_bottom).offset(margin);
        make.right.mas_equalTo(-margin);
        make.height.mas_equalTo(0);
    }];


    [_commentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.top.mas_equalTo(_contenView.mas_bottom).offset(margin);
        make.right.mas_equalTo(-margin);
        
    }];

    

    [_rewardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(margin);
        make.top.mas_equalTo(_commentView.mas_bottom).offset(margin);
        make.right.mas_equalTo(-margin);
        make.height.mas_equalTo(40);
    }];
    

    UIButton *userinforBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    userinforBtn.frame = CGRectMake(margin, margin, 150, 36);
    [userinforBtn addTarget:self action:@selector(userInfoClick:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:userinforBtn];
    
    _contentLb.numberOfLines = 3;
    _contentLb.preferredMaxLayoutWidth = ScreenWidth - margin*2;
    [_contentLb sizeToFit];

    _headerImageview.layer.masksToBounds = YES;
    _headerImageview.layer.cornerRadius = 36/2;
    
   
    _contenView.userInteractionEnabled = YES;
    _contenView.backgroundColor = [UIColor whiteColor];
    _rewardView.backgroundColor = [UIColor cyanColor];
    

    _memberLb.text = @"lv.26";
    _memberLb.font =[UIFont fontWithName:@"339-CAI978" size:5];
    _memberLb.textColor = [UIColor whiteColor];
    _memberLb.backgroundColor = UIColorFromHex(0xFFBC48);
    
    
    
    @weakify(self);
    _rewardView.zanBlock = ^{
        @strongify(self);
        if (self.zanBlock) {
            self.zanBlock();
        }
    };
    
    _rewardView.liwuBlock = ^{
        @strongify(self);
        if (self.liwuBlock) {
            self.liwuBlock();
        }
    };
    
    _rewardView.commentBlock = ^{
        @strongify(self);
        if (self.commentBlock) {
            self.commentBlock();
        }
    };

    

    self.hyb_lastViewInCell = _rewardView;
    self.hyb_bottomOffsetToCell = margin;
}

-(void)setModel:(WKContentListModel *)model{
    _model = model;
    [_headerImageview sd_setImageWithURL:[NSURL URLWithString:model.headerImagUrl] placeholderImage:nil];
    _nameLb.text = BLANKSTRING(model.name);
    _timeLb.text = BLANKSTRING(model.membe);
    _contentLb.text = BLANKSTRING(model.content);
    
    [_contenView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0);
    }];
    [_contenView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];


   
    
    
    
    //图文
    if (model.type == 0) {
        CGFloat picContainerTopMargin = 0;
//        [_contenView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.width.mas_equalTo(ScreenWidth-24);
//            make.height.mas_equalTo(0);
//        }];
        _contenView.backgroundColor = [UIColor magentaColor];
        _contenView.userInteractionEnabled = YES;

        if (model.images.count>0) {
            picContainerTopMargin = 10;
            
            CGFloat photo_height = [self getPhotoHeightWothImgs:model.images];
            
            [_contenView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(photo_height);
            }];
            
             WKContenCellPhotoView *photoView = [[WKContenCellPhotoView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, photo_height)];
            [_contenView addSubview:photoView];
             photoView.picPathStringsArray = model.images;

        }

    }
    
    //视频
    if (model.type == 1) {
          _contenView.backgroundColor = [UIColor blueColor];
        if (model.video.length>0) {
            WKContenCellViedeoView *videoView = [[WKContenCellViedeoView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-24, 195)];
            [_contenView addSubview:videoView];
            videoView.playVideoBlock = ^{
                if (self.playVideoBlock) {
                    self.playVideoBlock();
                }
            };
            [_contenView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(195);
            }];
        }
    }
    
   
    //问答
    if (model.type == 2) {
          _contenView.backgroundColor = [UIColor orangeColor];
        if (model.questionStr.length>0) {
            CGFloat quesitionHeight = 0;
            quesitionHeight = [model.questionStr boundingRectWithSize:CGSizeMake(ScreenWidth-24, MAXFLOAT) font:PFSC_RegularFont(12) lineSpacing:5].height;
        
            WKContenCellQuestionView *questionView = [[WKContenCellQuestionView alloc]init];
            [_contenView addSubview:questionView];
            
            questionView.questionStr = model.questionStr;
            
            [questionView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.width.mas_equalTo(0);
                make.height.mas_equalTo(quesitionHeight);
            }];
            
            [_contenView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(quesitionHeight);
            }];
            
        }
    }

 
    

    
    
    _commentView.commentStr = BLANKSTRING(model.comment);
    
    CGFloat commentHeight = 0;
    if (model.comment.length>0) {
        commentHeight = [model.comment boundingRectWithSize:CGSizeMake(ScreenWidth-24-16, MAXFLOAT) font:PFSC_RegularFont(12) lineSpacing:5 maxLines:3];
        commentHeight += 30;
        [_commentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_contenView.mas_bottom).offset(12);
            make.height.mas_equalTo(commentHeight);
        }];
       
        
    }else{
        
        [_commentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_contenView.mas_bottom).offset(0);
            make.height.mas_equalTo(commentHeight);
        }];
        
    }
    

}


#pragma mark - 点击关注
- (void)guanzhuClick:(UIButton*)sender{
    sender.selected = !sender.selected;
    _guanzhuBtn.backgroundColor = sender.selected == YES ? UIColorFromHex(0xF54050) : [UIColor whiteColor];
//    _guanzhuBtn.layer.borderColor = sender.selected == YES ? UIColorFromHex(0xF54050).CGColor : [UIColor whiteColor].CGColor;
}

#pragma mark - 点击用户头像
- (void)userInfoClick:(UIButton*)sender{
    if (self.userBlock) {
        self.userBlock();
    }
}

- (CGFloat)getPhotoHeightWothImgs:(NSArray*)imgs{
    CGFloat image_width = (ScreenWidth-24-10)/3;
    NSInteger photohight=0.0;
    if (imgs.count>3&&imgs.count<=6) {
        photohight = image_width*2+5;
    }else if (imgs.count<=3){
        photohight = image_width;
    }else if(imgs.count<=9 &&imgs.count>6){
        photohight = image_width*3+5*2;
        
    }else{
        photohight = 0;
    }
    return photohight;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
